package com.sjsm.sjsmscenerecord.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

import com.sjsm.sjsmscenerecord.bean.Action;
import com.sjsm.sjsmscenerecord.bean.MyCircle;
import com.sjsm.sjsmscenerecord.bean.MyRect;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/8/23.
 */

public class MyImageView extends SurfaceView implements SurfaceHolder.Callback{
    //获取此view的表面类,在表面编辑像素，并监控变化到表面
    private SurfaceHolder mSurfaceHolder = null;
    private Paint mPaint;
    //封装的图片的基类
    private List<Action> mActions;
    //当前动画的实例信息的类
    private Action curAction;
    //当前颜色,粗细
    private int currentColor = Color.RED;
    private int currentSize = 3;
    /**
     * 选择图片类型,可以是框框也可以是圈圈,默认是圈圈
     */
    private ActionType photoType = ActionType.Circle;
    private Bitmap bitMap;
    private int viewTop;

    public ActionType getPhotoType() {
        return photoType;
    }
    public void setPhotoType(ActionType photoType) {
        this.photoType = photoType;
    }

    public MyImageView(AppCompatActivity context, Bitmap bitmap){
        this(context);
        this.bitMap = bitmap;
        init( context);
    }


    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Activity context) {
        //获取当前表面编辑类
        mSurfaceHolder = getHolder();
        //添加监听
        mSurfaceHolder.addCallback(this);
        //设置可以获取焦点
        setFocusable(true);
        //画笔
        mPaint = new Paint();
        //颜色
        mPaint.setColor(Color.RED);
        //粗细
        mPaint.setStrokeWidth(3);
        //获取图片宽高信息
        int width = bitMap.getWidth();
        int height = bitMap.getHeight();
// 设置想要的大小
        int newWidth = context.getWindowManager().getDefaultDisplay().getWidth();
        int newHeight =context.getWindowManager().getDefaultDisplay().getHeight();
// 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
        bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
        viewTop = context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //当surface对象创建后，该方法就会被立即调用。
        //获取到表面的画布
        Canvas canvas = mSurfaceHolder.lockCanvas();
        //设置画布颜色

        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitMap,0,0,null);
        //结束锁定,并且提交改变,提交画面
        mSurfaceHolder.unlockCanvasAndPost(canvas);
        //存储图片的集合
        mActions = new ArrayList<Action>();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //当surface发生任何结构性的变化时（格式或者大小），该方法就会被立即调用。

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
       // 当surface对象在将要销毁前，该方法会被立即调用。
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_CANCEL) {
            return false;
        }
        //获取当前点相对屏幕左上的位置
        float touchX = event.getRawX();
        float touchY = event.getRawY()+viewTop;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //获取到当前的画笔
                setCurAction(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                //锁定页面,准备开始编辑页面
                Canvas canvas = mSurfaceHolder.lockCanvas();
                //设置背景为透明
                canvas.drawColor(Color.WHITE);
                canvas.drawBitmap(bitMap,0,0,null);
                //获取到里边所有的图片并且画出来
                for (Action a : mActions) {
                    a.draw(canvas);
                }
                //移动到这个位置,里边获取到当前圆的中心位置
                curAction.move(touchX, touchY+viewTop);
                //调用封装类,画出这个图片
                curAction.draw(canvas);
                //解锁页面,结束编辑
                mSurfaceHolder.unlockCanvasAndPost(canvas);
                break;
            case MotionEvent.ACTION_UP:
                //添加这个图片,以备以后删除
                mActions.add(curAction);
                curAction = null;
                break;
        }
        return true;
    }



    // 得到当前画笔的类型，并进行实例
    public void  setCurAction(float x, float y) {
        switch (photoType) {
            case Rect:
                curAction = new MyRect(x, y, currentSize, currentColor);
                break;
            case Circle:
                curAction = new MyCircle(x, y, currentSize, currentColor);
                break;
        }
    }

    public enum ActionType {
        Rect, Circle
    }

    /**
     * 获取画布的截图
     * @return
     */
    public Bitmap getBitmap() {
       Bitmap bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        doDraw(canvas,bmp);
        return bmp;
    }

    public void doDraw(Canvas canvas,Bitmap bmp) {
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitMap,0,0,null);
        for (Action a : mActions) {
            a.draw(canvas);
        }
        canvas.drawBitmap(bmp, 0, 0, mPaint);
    }


    /**
     * 回退
     * @return
     */
    public boolean back() {
        if (mActions != null && mActions.size() > 0) {
            mActions.remove(mActions.size() - 1);
            Canvas canvas = mSurfaceHolder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitMap,0,0,null);
            for (Action a : mActions) {
                a.draw(canvas);
            }
            mSurfaceHolder.unlockCanvasAndPost(canvas);
            return true;
        }
        return false;
    }
}
